﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using Utility;
namespace Service
{
    /// <summary>
    /// 功能：MenuConfig类数据访问
    /// 创建人：Wilson
    /// 创建时间：2013-1-29   
    /// </summary>
    public class MenuConfigService1 : IDisposable
    {
        #region 公共
        private System.ComponentModel.Component component = new System.ComponentModel.Component();

        private bool disposed = false;

        ~MenuConfigService1()
        {
            Dispose(false);
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {

                if (disposing)
                {

                    component.Dispose();
                }

                disposed = true;

            }
        }
        #endregion
				
        #region##通过主键ID得到MenuConfig
        /// <summary>
        /// 功能：通过ID得到MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public MenuConfig GetById(int configid)
        {            
            MenuConfig obj = null;
            string sql = "select * from MenuConfig where ConfigId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            
            SqlDataReader reader=SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
            if (reader.Read())
            {
                obj = new MenuConfig();				
				obj.ConfigId = (int)reader["ConfigId"];
				obj.MenuId = (int)reader["MenuId"];
				obj.RoleId = (int)reader["RoleId"];
				
            }
            return obj;
        }
        #endregion
		
		#region##通过主键ID删除MenuConfig
        /// <summary>
        /// 功能：通过主键ID删除MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(int configid)
		{
			int i = 0;
			string sql = "delete from MenuConfig where ConfigId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
			return i;
		}
        /// <summary>
        /// 功能：通过主键ID删除MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(SqlTransaction transaction,int configid)
		{
			int i = 0;
			string sql = "delete from MenuConfig where ConfigId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
			return i;
		}
		#endregion			
		
        #region##添加MenuConfig
        /// <summary>
        /// 功能：添加MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(MenuConfig menuconfig)
        {
            int result = 0;
            string sql = "insert into MenuConfig(ConfigId,MenuId,RoleId) values(@ConfigId,@MenuId,@RoleId)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@ConfigId",menuconfig.ConfigId),
				new SqlParameter("@MenuId",menuconfig.MenuId),
				new SqlParameter("@RoleId",menuconfig.RoleId)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：添加MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(SqlTransaction transaction,MenuConfig menuconfig)
        {
            int result = 0;
            string sql = "insert into MenuConfig(ConfigId,MenuId,RoleId) values(@ConfigId,@MenuId,@RoleId)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@ConfigId",menuconfig.ConfigId),
				new SqlParameter("@MenuId",menuconfig.MenuId),
				new SqlParameter("@RoleId",menuconfig.RoleId)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##得到MenuConfig集合
		/// <summary>
        /// 功能：得到MenuConfig集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<MenuConfig> GetList()
		{
		    IList<MenuConfig> list = new List<MenuConfig>();
            MenuConfig obj = null;				
			string sql = "select * from MenuConfig";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql);
			while(reader.Read())
			{
				obj = new MenuConfig();	
				obj.ConfigId = (int)reader["ConfigId"];
				obj.MenuId = (int)reader["MenuId"];
				obj.RoleId = (int)reader["RoleId"];
				list.Add(obj);				
			}
            return list;
		}
		#endregion
		
		#region##修改MenuConfig
        /// <summary>
        /// 功能：修改MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModMenuConfig(MenuConfig menuconfig)
        {
            int result = 0;
            string sql = "update MenuConfig set ConfigId = @ConfigId,MenuId = @MenuId,RoleId = @RoleId where ConfigId = @ConfigId";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@ConfigId",menuconfig.ConfigId),
				new SqlParameter("@MenuId",menuconfig.MenuId),
				new SqlParameter("@RoleId",menuconfig.RoleId)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：修改MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModMenuConfig(SqlTransaction transaction,MenuConfig menuconfig)
        {
            int result = 0;
            string sql = "update MenuConfig set ConfigId = @ConfigId,MenuId = @MenuId,RoleId = @RoleId where ConfigId = @ConfigId";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@ConfigId",menuconfig.ConfigId),
				new SqlParameter("@MenuId",menuconfig.MenuId),
				new SqlParameter("@RoleId",menuconfig.RoleId)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##统计MenuConfig
		/// <summary>
        /// 修改MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			int i = 0;
            string sql = "select count(*) from MenuConfig";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
		}
		/// <summary>
        /// 修改MenuConfig
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            int i=0;
            string sql="select count(ConfigId) from MenuConfig";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
        }
		#endregion
        #region##自定义MenuConfig
        public IList<MenuConfig> GetByRole(int RoleId)
        {
            IList<MenuConfig> list = new List<MenuConfig>();
            MenuConfig obj = null;
            string sql = "select * from MenuConfig where RoleId = @RoleId";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@RoleId",RoleId)
            };

            SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString, CommandType.Text, sql, sp);
            while(reader.Read())
            {
                obj = new MenuConfig();
                obj.ConfigId = (int)reader["ConfigId"];
                obj.MenuId = (int)reader["MenuId"];
                obj.RoleId = (int)reader["RoleId"];
                list.Add(obj);

            }
            return list;
        }
        #endregion
                
    }
}
